package com.wu.server.service;

import com.wu.server.code.Code;
import com.wu.server.config.EncodeJson;
import com.wu.server.dao.GoodsTypeRepository;
import com.wu.server.websocket.SelfPercentageWebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.websocket.EncodeException;
import java.util.HashMap;

/**
 * @author Wugs
 * @version 1.0
 * @date 2021/3/4 22:13
 */
@Service
public class SelfPercentageServer {
    @Autowired
    GoodsTypeRepository goodsTypeRepository;
    @Autowired
    EncodeJson encodeJson;
    @Autowired
    Code code;
    @Async
    // TODO: 2021/3/4 外部访问接口
    public void execute(){
        SelfPercentage();
    }

    /**
     * // TODO: 2021/3/6 获取自营比例数据
     */
    private void SelfPercentage(){
        Integer self = goodsTypeRepository.getSelf();
        Integer summary = goodsTypeRepository.getSummary();
        HashMap<String,Integer> data = new HashMap<String, Integer>();
        HashMap<String,Object> result = new HashMap<>();
        data.put("self",self);
        data.put("summary",summary);
        result.put("data",data);
        HashMap<String,Object> meta = new HashMap<>();
        meta.put("code", code.SELF);
        meta.put("msg",code.getMsg(code.SELF));
        result.put("meta",meta);
        try {
            SelfPercentageWebSocket.sendMessage(encodeJson.encode(result));
        } catch (EncodeException e) {
            e.printStackTrace();
        }

    }
}
